setwd("/Users/msbell/Documents/My Documents/Minnesota/Work/Research/Security Dilemma/Publication version/Replication/")
library(gplots)

China<-read.csv("China2020_for_analysis.csv")
US<-read.csv("US2020_for_analysis.csv")
China23<-read.csv("Ch_2023_for_analysis.csv")
US23<-read.csv("US_2023_for_analysis.csv")

USgCh<-US23[US23$USgivenCh==1,]
USgUS<-US23[US23$USgivenUS==1,]

ChgCh<-China23[China23$ChgivenCh==1,]
ChgUS<-China23[China23$ChgivenUS==1,]



####ANALYSIS FOR ONLINE APPENDIX

#### Party, Current or Past CCP member=1. WHAT WAS FEMALE WILL NOW BE CCP

#Treatment effects in China 2020, CCP
a<-t.test(China[China$control==1 & China$CCP==1,]$defspend,China[China$offdef==1 & China$CCP==1,]$defspend, var.equal=F)
b<-t.test(China[China$control==1 & China$CCP==1,]$defspend,China[China$secdim==1 & China$CCP==1,]$defspend, var.equal=F)
c<-t.test(China[China$control==1 & China$CCP==1,]$defspend,China[China$nukes==1 & China$CCP==1,]$defspend, var.equal=F)
d<-t.test(China[China$control==1 & China$CCP==1,]$defspend,China[China$interd==1 & China$CCP==1,]$defspend, var.equal=F)

e<-t.test(China[China$control==1 & China$CCP==1,]$USisdef,China[China$offdef==1 & China$CCP==1,]$USisdef, var.equal=F)
f<-t.test(China[China$control==1 & China$CCP==1,]$USisdef,China[China$secdim==1 & China$CCP==1,]$USisdef, var.equal=F)
g<-t.test(China[China$control==1 & China$CCP==1,]$USisdef,China[China$nukes==1 & China$CCP==1,]$USisdef, var.equal=F)
h<-t.test(China[China$control==1 & China$CCP==1,]$USisdef,China[China$interd==1 & China$CCP==1,]$USisdef, var.equal=F)

i<-t.test(China[China$control==1 & China$CCP==1,]$USisthreat,China[China$offdef==1 & China$CCP==1,]$USisthreat, var.equal=F)
j<-t.test(China[China$control==1 & China$CCP==1,]$USisthreat,China[China$secdim==1 & China$CCP==1,]$USisthreat, var.equal=F)
k<-t.test(China[China$control==1 & China$CCP==1,]$USisthreat,China[China$nukes==1 & China$CCP==1,]$USisthreat, var.equal=F)
l<-t.test(China[China$control==1 & China$CCP==1,]$USisthreat,China[China$interd==1 & China$CCP==1,]$USisthreat, var.equal=F)

#Treatment effects in China 2020, not CCP
a1<-t.test(China[China$control==1 & China$CCP==0,]$defspend,China[China$offdef==1 & China$CCP==0,]$defspend, var.equal=F)
b1<-t.test(China[China$control==1 & China$CCP==0,]$defspend,China[China$secdim==1 & China$CCP==0,]$defspend, var.equal=F)
c1<-t.test(China[China$control==1 & China$CCP==0,]$defspend,China[China$nukes==1 & China$CCP==0,]$defspend, var.equal=F)
d1<-t.test(China[China$control==1 & China$CCP==0,]$defspend,China[China$interd==1 & China$CCP==0,]$defspend, var.equal=F)

e1<-t.test(China[China$control==1 & China$CCP==0,]$USisdef,China[China$offdef==1 & China$CCP==0,]$USisdef, var.equal=F)
f1<-t.test(China[China$control==1 & China$CCP==0,]$USisdef,China[China$secdim==1 & China$CCP==0,]$USisdef, var.equal=F)
g1<-t.test(China[China$control==1 & China$CCP==0,]$USisdef,China[China$nukes==1 & China$CCP==0,]$USisdef, var.equal=F)
h1<-t.test(China[China$control==1 & China$CCP==0,]$USisdef,China[China$interd==1 & China$CCP==0,]$USisdef, var.equal=F)

i1<-t.test(China[China$control==1 & China$CCP==0,]$USisthreat,China[China$offdef==1 & China$CCP==0,]$USisthreat, var.equal=F)
j1<-t.test(China[China$control==1 & China$CCP==0,]$USisthreat,China[China$secdim==1 & China$CCP==0,]$USisthreat, var.equal=F)
k1<-t.test(China[China$control==1 & China$CCP==0,]$USisthreat,China[China$nukes==1 & China$CCP==0,]$USisthreat, var.equal=F)
l1<-t.test(China[China$control==1 & China$CCP==0,]$USisthreat,China[China$interd==1 & China$CCP==0,]$USisthreat, var.equal=F)


#Treatment effects in US 2020, republican
m<-t.test(US[US$control==1 & US$republican==1,]$defspend,US[US$offdef==1 & US$republican==1,]$defspend, var.equal=F)
n<-t.test(US[US$control==1 & US$republican==1,]$defspend,US[US$secdim==1 & US$republican==1,]$defspend, var.equal=F)
o<-t.test(US[US$control==1 & US$republican==1,]$defspend,US[US$nukes==1 & US$republican==1,]$defspend, var.equal=F)
p<-t.test(US[US$control==1 & US$republican==1,]$defspend,US[US$interd==1 & US$republican==1,]$defspend, var.equal=F)

q<-t.test(US[US$control==1 & US$republican==1,]$Chisdef,US[US$offdef==1 & US$republican==1,]$Chisdef, var.equal=F)
r<-t.test(US[US$control==1 & US$republican==1,]$Chisdef,US[US$secdim==1 & US$republican==1,]$Chisdef, var.equal=F)
s<-t.test(US[US$control==1 & US$republican==1,]$Chisdef,US[US$nukes==1 & US$republican==1,]$Chisdef, var.equal=F)
t<-t.test(US[US$control==1 & US$republican==1,]$Chisdef,US[US$interd==1 & US$republican==1,]$Chisdef, var.equal=F)

u<-t.test(US[US$control==1 & US$republican==1,]$Chisthreat,US[US$offdef==1 & US$republican==1,]$Chisthreat, var.equal=F)
v<-t.test(US[US$control==1 & US$republican==1,]$Chisthreat,US[US$secdim==1 & US$republican==1,]$Chisthreat, var.equal=F)
w<-t.test(US[US$control==1 & US$republican==1,]$Chisthreat,US[US$nukes==1 & US$republican==1,]$Chisthreat, var.equal=F)
x<-t.test(US[US$control==1 & US$republican==1,]$Chisthreat,US[US$interd==1 & US$republican==1,]$Chisthreat, var.equal=F)

#Treatment effects in US 2020, dem
m1<-t.test(US[US$control==1 & US$republican==0,]$defspend,US[US$offdef==1 & US$republican==0,]$defspend, var.equal=F)
n1<-t.test(US[US$control==1 & US$republican==0,]$defspend,US[US$secdim==1 & US$republican==0,]$defspend, var.equal=F)
o1<-t.test(US[US$control==1 & US$republican==0,]$defspend,US[US$nukes==1 & US$republican==0,]$defspend, var.equal=F)
p1<-t.test(US[US$control==1 & US$republican==0,]$defspend,US[US$interd==1 & US$republican==0,]$defspend, var.equal=F)

q1<-t.test(US[US$control==1 & US$republican==0,]$Chisdef,US[US$offdef==1 & US$republican==0,]$Chisdef, var.equal=F)
r1<-t.test(US[US$control==1 & US$republican==0,]$Chisdef,US[US$secdim==1 & US$republican==0,]$Chisdef, var.equal=F)
s1<-t.test(US[US$control==1 & US$republican==0,]$Chisdef,US[US$nukes==1 & US$republican==0,]$Chisdef, var.equal=F)
t1<-t.test(US[US$control==1 & US$republican==0,]$Chisdef,US[US$interd==1 & US$republican==0,]$Chisdef, var.equal=F)

u1<-t.test(US[US$control==1 & US$republican==0,]$Chisthreat,US[US$offdef==1 & US$republican==0,]$Chisthreat, var.equal=F)
v1<-t.test(US[US$control==1 & US$republican==0,]$Chisthreat,US[US$secdim==1 & US$republican==0,]$Chisthreat, var.equal=F)
w1<-t.test(US[US$control==1 & US$republican==0,]$Chisthreat,US[US$nukes==1 & US$republican==0,]$Chisthreat, var.equal=F)
x1<-t.test(US[US$control==1 & US$republican==0,]$Chisthreat,US[US$interd==1 & US$republican==0,]$Chisthreat, var.equal=F)

#Treatment effects in China 2023, CCP
aa<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$defspend,ChgUS[ChgUS$offdef==1 &  ChgUS$CCP==1,]$defspend, var.equal=F)
bb<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$defspend,ChgUS[ChgUS$secdim==1 &  ChgUS$CCP==1,]$defspend, var.equal=F)
cc<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$defspend,ChgUS[ChgUS$nukes==1 &  ChgUS$CCP==1,]$defspend, var.equal=F)
dd<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$defspend,ChgUS[ChgUS$interd==1 &  ChgUS$CCP==1,]$defspend, var.equal=F)

ee<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USdef,ChgUS[ChgUS$offdef==1 &  ChgUS$CCP==1,]$USdef, var.equal=F)
ff<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USdef,ChgUS[ChgUS$secdim==1 &  ChgUS$CCP==1,]$USdef, var.equal=F)
gg<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USdef,ChgUS[ChgUS$nukes==1 &  ChgUS$CCP==1,]$USdef, var.equal=F)
hh<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USdef,ChgUS[ChgUS$interd==1 &  ChgUS$CCP==1,]$USdef, var.equal=F)

ii<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USisthreat,ChgUS[ChgUS$offdef==1 &  ChgUS$CCP==1,]$USisthreat, var.equal=F)
jj<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USisthreat,ChgUS[ChgUS$secdim==1 &  ChgUS$CCP==1,]$USisthreat, var.equal=F)
kk<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USisthreat,ChgUS[ChgUS$nukes==1 &  ChgUS$CCP==1,]$USisthreat, var.equal=F)
ll<-t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USisthreat,ChgUS[ChgUS$interd==1 &  ChgUS$CCP==1,]$USisthreat, var.equal=F)

#Treatment effects in China 2023, not CCP
aa1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$defspend,ChgUS[ChgUS$offdef==1 & ChgUS$CCP==0,]$defspend, var.equal=F)
bb1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$defspend,ChgUS[ChgUS$secdim==1 & ChgUS$CCP==0,]$defspend, var.equal=F)
cc1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$defspend,ChgUS[ChgUS$nukes==1 & ChgUS$CCP==0,]$defspend, var.equal=F)
dd1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$defspend,ChgUS[ChgUS$interd==1 & ChgUS$CCP==0,]$defspend, var.equal=F)

ee1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USdef,ChgUS[ChgUS$offdef==1 & ChgUS$CCP==0,]$USdef, var.equal=F)
ff1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USdef,ChgUS[ChgUS$secdim==1 & ChgUS$CCP==0,]$USdef, var.equal=F)
gg1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USdef,ChgUS[ChgUS$nukes==1 & ChgUS$CCP==0,]$USdef, var.equal=F)
hh1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USdef,ChgUS[ChgUS$interd==1 & ChgUS$CCP==0,]$USdef, var.equal=F)

ii1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USisthreat,ChgUS[ChgUS$offdef==1 & ChgUS$CCP==0,]$USisthreat, var.equal=F)
jj1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USisthreat,ChgUS[ChgUS$secdim==1 & ChgUS$CCP==0,]$USisthreat, var.equal=F)
kk1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USisthreat,ChgUS[ChgUS$nukes==1 & ChgUS$CCP==0,]$USisthreat, var.equal=F)
ll1<-t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USisthreat,ChgUS[ChgUS$interd==1 & ChgUS$CCP==0,]$USisthreat, var.equal=F)

#Treatment effects in US 2023, republican
mm<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$defspend,USgCh[USgCh$offdef==1 & USgCh$republican==1,]$defspend, var.equal=F)
nn<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$defspend,USgCh[USgCh$secdim==1 & USgCh$republican==1,]$defspend, var.equal=F)
oo<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$defspend,USgCh[USgCh$nukes==1 & USgCh$republican==1,]$defspend, var.equal=F)
pp<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$defspend,USgCh[USgCh$interd==1 & USgCh$republican==1,]$defspend, var.equal=F)

qq<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chdef,USgCh[USgCh$offdef==1 & USgCh$republican==1,]$Chdef, var.equal=F)
rr<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chdef,USgCh[USgCh$secdim==1 & USgCh$republican==1,]$Chdef, var.equal=F)
ss<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chdef,USgCh[USgCh$nukes==1 & USgCh$republican==1,]$Chdef, var.equal=F)
tt<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chdef,USgCh[USgCh$interd==1 & USgCh$republican==1,]$Chdef, var.equal=F)

uu<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chisthreat,USgCh[USgCh$offdef==1 & USgCh$republican==1,]$Chisthreat, var.equal=F)
vv<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chisthreat,USgCh[USgCh$secdim==1 & USgCh$republican==1,]$Chisthreat, var.equal=F)
ww<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chisthreat,USgCh[USgCh$nukes==1 & USgCh$republican==1,]$Chisthreat, var.equal=F)
xx<-t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chisthreat,USgCh[USgCh$interd==1 & USgCh$republican==1,]$Chisthreat, var.equal=F)

#Treatment effects in US 2023, male
mm1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$defspend,USgCh[USgCh$offdef==1 & USgCh$republican==0,]$defspend, var.equal=F)
nn1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$defspend,USgCh[USgCh$secdim==1 & USgCh$republican==0,]$defspend, var.equal=F)
oo1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$defspend,USgCh[USgCh$nukes==1 & USgCh$republican==0,]$defspend, var.equal=F)
pp1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$defspend,USgCh[USgCh$interd==1 & USgCh$republican==0,]$defspend, var.equal=F)

qq1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chdef,USgCh[USgCh$offdef==1 & USgCh$republican==0,]$Chdef, var.equal=F)
rr1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chdef,USgCh[USgCh$secdim==1 & USgCh$republican==0,]$Chdef, var.equal=F)
ss1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chdef,USgCh[USgCh$nukes==1 & USgCh$republican==0,]$Chdef, var.equal=F)
tt1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chdef,USgCh[USgCh$interd==1 & USgCh$republican==0,]$Chdef, var.equal=F)

uu1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chisthreat,USgCh[USgCh$offdef==1 & USgCh$republican==0,]$Chisthreat, var.equal=F)
vv1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chisthreat,USgCh[USgCh$secdim==1 & USgCh$republican==0,]$Chisthreat, var.equal=F)
ww1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chisthreat,USgCh[USgCh$nukes==1 & USgCh$republican==0,]$Chisthreat, var.equal=F)
xx1<-t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chisthreat,USgCh[USgCh$interd==1 & USgCh$republican==0,]$Chisthreat, var.equal=F)



pdf("effects_bar_party_appendix.pdf", height=15, width=15)

par(mfrow=c(3,1))

# Def spending

matrix1<-matrix(data=c(m$estimate[1],m$estimate[2],n$estimate[2],o$estimate[2],p$estimate[2],m1$estimate[1],m1$estimate[2],n1$estimate[2],o1$estimate[2],p1$estimate[2], mm$estimate[1],mm$estimate[2],nn$estimate[2],oo$estimate[2],pp$estimate[2],mm1$estimate[1],mm1$estimate[2],nn1$estimate[2],oo1$estimate[2],pp1$estimate[2],a$estimate[1],a$estimate[2],b$estimate[2],c$estimate[2],d$estimate[2],a1$estimate[1],a1$estimate[2],b1$estimate[2],c1$estimate[2],d1$estimate[2],aa$estimate[1],aa$estimate[2],bb$estimate[2],cc$estimate[2],dd$estimate[2],aa1$estimate[1],aa1$estimate[2],bb1$estimate[2],cc1$estimate[2],dd1$estimate[2]), ncol=8, byrow=F) ##create matrix for barplot

lower1<-matrix(data=c(t.test(US[US$control==1 & US$republican==1,]$defspend)$conf.int[1], t.test(US[US$offdef==1 & US$republican==1,]$defspend)$conf.int[1], t.test(US[US$secdim==1 & US$republican==1,]$defspend)$conf.int[1],t.test(US[US$nukes==1 & US$republican==1,]$defspend)$conf.int[1],t.test(US[US$interd==1 & US$republican==1,]$defspend)$conf.int[1], t.test(US[US$control==1 & US$republican==0,]$defspend)$conf.int[1],t.test(US[US$offdef==1 & US$republican==0,]$defspend)$conf.int[1], t.test(US[US$secdim==1 & US$republican==0,]$defspend)$conf.int[1],t.test(US[US$nukes==1 & US$republican==0,]$defspend)$conf.int[1],t.test(US[US$interd==1 & US$republican==0,]$defspend)$conf.int[1], t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$defspend)$conf.int[1], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==1,]$defspend)$conf.int[1], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==1,]$defspend)$conf.int[1], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==1,]$defspend)$conf.int[1], t.test(USgCh[USgCh$interd==1 & USgCh$republican==1,]$defspend)$conf.int[1], t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$defspend)$conf.int[1], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==0,]$defspend)$conf.int[1], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==0,]$defspend)$conf.int[1], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==0,]$defspend)$conf.int[1], t.test(USgCh[USgCh$interd==1 & USgCh$republican==0,]$defspend)$conf.int[1], t.test(China[China$control==1 & China$CCP==1,]$defspend)$conf.int[1],t.test(China[China$offdef==1 & China$CCP==1,]$defspend)$conf.int[1], t.test(China[China$secdim==1 & China$CCP==1,]$defspend)$conf.int[1],t.test(China[China$nukes==1 & China$CCP==1,]$defspend)$conf.int[1],t.test(China[China$interd==1 & China$CCP==1,]$defspend)$conf.int[1], t.test(China[China$control==1 & China$CCP==0,]$defspend)$conf.int[1],t.test(China[China$offdef==1 & China$CCP==0,]$defspend)$conf.int[1], t.test(China[China$secdim==1 & China$CCP==0,]$defspend)$conf.int[1],t.test(China[China$nukes==1 & China$CCP==0,]$defspend)$conf.int[1],t.test(China[China$interd==1 & China$CCP==0,]$defspend)$conf.int[1],t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$offdef==1 &  ChgUS$CCP==1,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$secdim==1 &  ChgUS$CCP==1,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$nukes==1 &  ChgUS$CCP==1,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$interd==1 &  ChgUS$CCP==1,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$offdef==1 & ChgUS$CCP==0,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$secdim==1 & ChgUS$CCP==0,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$nukes==1 & ChgUS$CCP==0,]$defspend)$conf.int[1], t.test(ChgUS[ChgUS$interd==1 & ChgUS$CCP==0,]$defspend)$conf.int[1]), ncol=8, byrow=F)

upper1<-matrix(data=c(t.test(US[US$control==1 & US$republican==1,]$defspend)$conf.int[2], t.test(US[US$offdef==1 & US$republican==1,]$defspend)$conf.int[2], t.test(US[US$secdim==1 & US$republican==1,]$defspend)$conf.int[2],t.test(US[US$nukes==1 & US$republican==1,]$defspend)$conf.int[2],t.test(US[US$interd==1 & US$republican==1,]$defspend)$conf.int[2], t.test(US[US$control==1 & US$republican==0,]$defspend)$conf.int[2],t.test(US[US$offdef==1 & US$republican==0,]$defspend)$conf.int[2], t.test(US[US$secdim==1 & US$republican==0,]$defspend)$conf.int[2],t.test(US[US$nukes==1 & US$republican==0,]$defspend)$conf.int[2],t.test(US[US$interd==1 & US$republican==0,]$defspend)$conf.int[2], t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$defspend)$conf.int[2], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==1,]$defspend)$conf.int[2], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==1,]$defspend)$conf.int[2], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==1,]$defspend)$conf.int[2], t.test(USgCh[USgCh$interd==1 & USgCh$republican==1,]$defspend)$conf.int[2], t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$defspend)$conf.int[2], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==0,]$defspend)$conf.int[2], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==0,]$defspend)$conf.int[2], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==0,]$defspend)$conf.int[2], t.test(USgCh[USgCh$interd==1 & USgCh$republican==0,]$defspend)$conf.int[2], t.test(China[China$control==1 & China$CCP==1,]$defspend)$conf.int[2],t.test(China[China$offdef==1 & China$CCP==1,]$defspend)$conf.int[2], t.test(China[China$secdim==1 & China$CCP==1,]$defspend)$conf.int[2],t.test(China[China$nukes==1 & China$CCP==1,]$defspend)$conf.int[2],t.test(China[China$interd==1 & China$CCP==1,]$defspend)$conf.int[2], t.test(China[China$control==1 & China$CCP==0,]$defspend)$conf.int[2],t.test(China[China$offdef==1 & China$CCP==0,]$defspend)$conf.int[2], t.test(China[China$secdim==1 & China$CCP==0,]$defspend)$conf.int[2],t.test(China[China$nukes==1 & China$CCP==0,]$defspend)$conf.int[2],t.test(China[China$interd==1 & China$CCP==0,]$defspend)$conf.int[2],t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$offdef==1 &  ChgUS$CCP==1,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$secdim==1 &  ChgUS$CCP==1,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$nukes==1 &  ChgUS$CCP==1,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$interd==1 &  ChgUS$CCP==1,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$offdef==1 & ChgUS$CCP==0,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$secdim==1 & ChgUS$CCP==0,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$nukes==1 & ChgUS$CCP==0,]$defspend)$conf.int[2], t.test(ChgUS[ChgUS$interd==1 & ChgUS$CCP==0,]$defspend)$conf.int[2]), ncol=8, byrow=F)

barplot2(matrix1, beside=T, col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90"), space=c(0,1.5), ylim=c(1,7), axes=T, ylab="Should [your country]'s military spending increase?", names.arg=c("US (2020), Rep", "US (2020), Dem", "US (2023), Rep", "US (2023), Dem", "China (2020), CCP", "China (2020), Not CCP", "China (2023), CCP", "China (2023), Not CCP"), plot.ci=T, ci.l=lower1, ci.u=upper1, xpd=F, cex=1, cex.lab=1.4, cex.names=1.4, cex.main=2, main= "Support for Increasing Military Spending (7 = Increase greatly; 1 = Decrease greatly)")

segments(x0=1, x1=7, y0=m$estimate[1], y1=m$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=7.5, x1=13.5, y0=m1$estimate[1], y1=m1$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=14, x1=20, y0=mm$estimate[1], y1=mm$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=20.5, x1=26.5, y0=mm1$estimate[1], y1=mm1$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=27, x1=33, y0=a$estimate[1], y1=a$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=33.5, x1=39.5, y0=a1$estimate[1], y1=a1$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=40, x1=46, y0=aa$estimate[1], y1=aa$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=46.5, x1=52.5, y0=aa1$estimate[1], y1=aa1$estimate[1], lty=3, col="BLACK", lwd=1.5)

segments(x0=1, x1=52.5, y0=1, y1=1, lty=1, col="BLACK")

legend(x=1, y=7.1, lty=c(NA, NA, NA, NA, NA, 3), lwd=c(NA, NA, NA, NA, NA, 1.5), pch=c(15,15,15,15,15, NA), col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90", "BLACK"), legend=c("Control group", "Offense defense", "Reciprocal", "Nuclear Weapons", "Interdependence","Mean within control group" ), bty="n", cex=1.1)


# Offensive/Def motivations


matrix2<-matrix(data=c(q$estimate[1],q$estimate[2],r$estimate[2],s$estimate[2],t$estimate[2],q1$estimate[1],q1$estimate[2],r1$estimate[2],s1$estimate[2],t1$estimate[2], qq$estimate[1],qq$estimate[2],rr$estimate[2],ss$estimate[2],tt$estimate[2],qq1$estimate[1],qq1$estimate[2],rr1$estimate[2],ss1$estimate[2],tt1$estimate[2],e$estimate[1],e$estimate[2],f$estimate[2],g$estimate[2],h$estimate[2],e1$estimate[1],e1$estimate[2],f1$estimate[2],g1$estimate[2],h1$estimate[2],ee$estimate[1],ee$estimate[2],ff$estimate[2],gg$estimate[2],hh$estimate[2],ee1$estimate[1],ee1$estimate[2],ff1$estimate[2],gg1$estimate[2],hh1$estimate[2]), ncol=8, byrow=F) ##create matrix for barplot

lower2<-matrix(data=c(t.test(US[US$control==1 & US$republican==1,]$Chisdef)$conf.int[1], t.test(US[US$offdef==1 & US$republican==1,]$Chisdef)$conf.int[1], t.test(US[US$secdim==1 & US$republican==1,]$Chisdef)$conf.int[1],t.test(US[US$nukes==1 & US$republican==1,]$Chisdef)$conf.int[1],t.test(US[US$interd==1 & US$republican==1,]$Chisdef)$conf.int[1], t.test(US[US$control==1 & US$republican==0,]$Chisdef)$conf.int[1],t.test(US[US$offdef==1 & US$republican==0,]$Chisdef)$conf.int[1], t.test(US[US$secdim==1 & US$republican==0,]$Chisdef)$conf.int[1],t.test(US[US$nukes==1 & US$republican==0,]$Chisdef)$conf.int[1],t.test(US[US$interd==1 & US$republican==0,]$Chisdef)$conf.int[1], t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==1,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==1,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==1,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$interd==1 & USgCh$republican==1,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==0,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==0,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==0,]$Chdef)$conf.int[1], t.test(USgCh[USgCh$interd==1 & USgCh$republican==0,]$Chdef)$conf.int[1], t.test(China[China$control==1 & China$CCP==1,]$USisdef)$conf.int[1],t.test(China[China$offdef==1 & China$CCP==1,]$USisdef)$conf.int[1], t.test(China[China$secdim==1 & China$CCP==1,]$USisdef)$conf.int[1],t.test(China[China$nukes==1 & China$CCP==1,]$USisdef)$conf.int[1],t.test(China[China$interd==1 & China$CCP==1,]$USisdef)$conf.int[1], t.test(China[China$control==1 & China$CCP==0,]$USisdef)$conf.int[1],t.test(China[China$offdef==1 & China$CCP==0,]$USisdef)$conf.int[1], t.test(China[China$secdim==1 & China$CCP==0,]$USisdef)$conf.int[1],t.test(China[China$nukes==1 & China$CCP==0,]$USisdef)$conf.int[1],t.test(China[China$interd==1 & China$CCP==0,]$USisdef)$conf.int[1],t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$offdef==1 &  ChgUS$CCP==1,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$secdim==1 &  ChgUS$CCP==1,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$nukes==1 &  ChgUS$CCP==1,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$interd==1 &  ChgUS$CCP==1,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$offdef==1 & ChgUS$CCP==0,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$secdim==1 & ChgUS$CCP==0,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$nukes==1 & ChgUS$CCP==0,]$USdef)$conf.int[1], t.test(ChgUS[ChgUS$interd==1 & ChgUS$CCP==0,]$USdef)$conf.int[1]), ncol=8, byrow=F)


upper2<-matrix(data=c(t.test(US[US$control==1 & US$republican==1,]$Chisdef)$conf.int[2], t.test(US[US$offdef==1 & US$republican==1,]$Chisdef)$conf.int[2], t.test(US[US$secdim==1 & US$republican==1,]$Chisdef)$conf.int[2],t.test(US[US$nukes==1 & US$republican==1,]$Chisdef)$conf.int[2],t.test(US[US$interd==1 & US$republican==1,]$Chisdef)$conf.int[2], t.test(US[US$control==1 & US$republican==0,]$Chisdef)$conf.int[2],t.test(US[US$offdef==1 & US$republican==0,]$Chisdef)$conf.int[2], t.test(US[US$secdim==1 & US$republican==0,]$Chisdef)$conf.int[2],t.test(US[US$nukes==1 & US$republican==0,]$Chisdef)$conf.int[2],t.test(US[US$interd==1 & US$republican==0,]$Chisdef)$conf.int[2], t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==1,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==1,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==1,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$interd==1 & USgCh$republican==1,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==0,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==0,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==0,]$Chdef)$conf.int[2], t.test(USgCh[USgCh$interd==1 & USgCh$republican==0,]$Chdef)$conf.int[2], t.test(China[China$control==1 & China$CCP==1,]$USisdef)$conf.int[2],t.test(China[China$offdef==1 & China$CCP==1,]$USisdef)$conf.int[2], t.test(China[China$secdim==1 & China$CCP==1,]$USisdef)$conf.int[2],t.test(China[China$nukes==1 & China$CCP==1,]$USisdef)$conf.int[2],t.test(China[China$interd==1 & China$CCP==1,]$USisdef)$conf.int[2], t.test(China[China$control==1 & China$CCP==0,]$USisdef)$conf.int[2],t.test(China[China$offdef==1 & China$CCP==0,]$USisdef)$conf.int[2], t.test(China[China$secdim==1 & China$CCP==0,]$USisdef)$conf.int[2],t.test(China[China$nukes==1 & China$CCP==0,]$USisdef)$conf.int[2],t.test(China[China$interd==1 & China$CCP==0,]$USisdef)$conf.int[2],t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$offdef==1 &  ChgUS$CCP==1,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$secdim==1 &  ChgUS$CCP==1,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$nukes==1 &  ChgUS$CCP==1,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$interd==1 &  ChgUS$CCP==1,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$offdef==1 & ChgUS$CCP==0,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$secdim==1 & ChgUS$CCP==0,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$nukes==1 & ChgUS$CCP==0,]$USdef)$conf.int[2], t.test(ChgUS[ChgUS$interd==1 & ChgUS$CCP==0,]$USdef)$conf.int[2]), ncol=8, byrow=F)

barplot2(matrix2, beside=T, col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90"), space=c(0,1.5), ylim=c(1,7), axes=T, ylab="Are [other country]'s motivations offensive or defensive?", names.arg=c("US (2020), Rep", "US (2020), Dem", "US (2023), Rep", "US (2023), Dem", "China (2020), CCP", "China (2020), Not CCP", "China (2023), CCP", "China (2023), Not CCP"), plot.ci=T, ci.l=lower2, ci.u=upper2, xpd=F, cex=1, cex.lab=1.4, cex.names=1.4, cex.main=2, main= "Perception of Offensive Motivations (7 = Purely offensive; 1 = Purely defensive)")


segments(x0=1, x1=7, y0=q$estimate[1], y1=q$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=7.5, x1=13.5, y0=q1$estimate[1], y1=q1$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=14, x1=20, y0=qq$estimate[1], y1=qq$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=20.5, x1=26.5, y0=qq1$estimate[1], y1=qq1$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=27, x1=33, y0=e$estimate[1], y1=e$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=33.5, x1=39.5, y0=e1$estimate[1], y1=e1$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=40, x1=46, y0=ee$estimate[1], y1=ee$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=46.5, x1=52.5, y0=ee1$estimate[1], y1=ee1$estimate[1], lty=3, col="BLACK", lwd=1.5)


segments(x0=1, x1=52.5, y0=1, y1=1, lty=1, col="BLACK")

legend(x=1, y=7.1, lty=c(NA, NA, NA, NA, NA, 3), lwd=c(NA, NA, NA, NA, NA, 1.5), pch=c(15,15,15,15,15, NA), col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90", "BLACK"), legend=c("Control group", "Offense defense", "Reciprocal", "Nuclear Weapons", "Interdependence","Mean within control group" ), bty="n", cex=1.1)


# Threat

matrix3<-matrix(data=c(u$estimate[1],u$estimate[2],v$estimate[2],w$estimate[2],x$estimate[2],u1$estimate[1],u1$estimate[2],v1$estimate[2],w1$estimate[2],x1$estimate[2], uu$estimate[1],uu$estimate[2],vv$estimate[2],ww$estimate[2],xx$estimate[2],uu1$estimate[1],uu1$estimate[2],vv1$estimate[2],ww1$estimate[2],xx1$estimate[2],i$estimate[1],i$estimate[2],j$estimate[2],k$estimate[2],l$estimate[2],i1$estimate[1],i1$estimate[2],j1$estimate[2],k1$estimate[2],l1$estimate[2],ii$estimate[1],ii$estimate[2],jj$estimate[2],kk$estimate[2],ll$estimate[2],ii1$estimate[1],ii1$estimate[2],jj1$estimate[2],kk1$estimate[2],ll1$estimate[2]), ncol=8, byrow=F) ##create matrix for barplot

lower3<-matrix(data=c(t.test(US[US$control==1 & US$republican==1,]$Chisthreat)$conf.int[1], t.test(US[US$offdef==1 & US$republican==1,]$Chisthreat)$conf.int[1], t.test(US[US$secdim==1 & US$republican==1,]$Chisthreat)$conf.int[1],t.test(US[US$nukes==1 & US$republican==1,]$Chisthreat)$conf.int[1],t.test(US[US$interd==1 & US$republican==1,]$Chisthreat)$conf.int[1], t.test(US[US$control==1 & US$republican==0,]$Chisthreat)$conf.int[1],t.test(US[US$offdef==1 & US$republican==0,]$Chisthreat)$conf.int[1], t.test(US[US$secdim==1 & US$republican==0,]$Chisthreat)$conf.int[1],t.test(US[US$nukes==1 & US$republican==0,]$Chisthreat)$conf.int[1],t.test(US[US$interd==1 & US$republican==0,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==1,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==1,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==1,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$interd==1 & USgCh$republican==1,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==0,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==0,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==0,]$Chisthreat)$conf.int[1], t.test(USgCh[USgCh$interd==1 & USgCh$republican==0,]$Chisthreat)$conf.int[1], t.test(China[China$control==1 & China$CCP==1,]$USisthreat)$conf.int[1],t.test(China[China$offdef==1 & China$CCP==1,]$USisthreat)$conf.int[1], t.test(China[China$secdim==1 & China$CCP==1,]$USisthreat)$conf.int[1],t.test(China[China$nukes==1 & China$CCP==1,]$USisthreat)$conf.int[1],t.test(China[China$interd==1 & China$CCP==1,]$USisthreat)$conf.int[1], t.test(China[China$control==1 & China$CCP==0,]$USisthreat)$conf.int[1],t.test(China[China$offdef==1 & China$CCP==0,]$USisthreat)$conf.int[1], t.test(China[China$secdim==1 & China$CCP==0,]$USisthreat)$conf.int[1],t.test(China[China$nukes==1 & China$CCP==0,]$USisthreat)$conf.int[1],t.test(China[China$interd==1 & China$CCP==0,]$USisthreat)$conf.int[1],t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$offdef==1 &  ChgUS$CCP==1,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$secdim==1 &  ChgUS$CCP==1,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$nukes==1 &  ChgUS$CCP==1,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$interd==1 &  ChgUS$CCP==1,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$offdef==1 & ChgUS$CCP==0,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$secdim==1 & ChgUS$CCP==0,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$nukes==1 & ChgUS$CCP==0,]$USisthreat)$conf.int[1], t.test(ChgUS[ChgUS$interd==1 & ChgUS$CCP==0,]$USisthreat)$conf.int[1]), ncol=8, byrow=F)

upper3<-matrix(data=c(t.test(US[US$control==1 & US$republican==1,]$Chisthreat)$conf.int[2], t.test(US[US$offdef==1 & US$republican==1,]$Chisthreat)$conf.int[2], t.test(US[US$secdim==1 & US$republican==1,]$Chisthreat)$conf.int[2],t.test(US[US$nukes==1 & US$republican==1,]$Chisthreat)$conf.int[2],t.test(US[US$interd==1 & US$republican==1,]$Chisthreat)$conf.int[2], t.test(US[US$control==1 & US$republican==0,]$Chisthreat)$conf.int[2],t.test(US[US$offdef==1 & US$republican==0,]$Chisthreat)$conf.int[2], t.test(US[US$secdim==1 & US$republican==0,]$Chisthreat)$conf.int[2],t.test(US[US$nukes==1 & US$republican==0,]$Chisthreat)$conf.int[2],t.test(US[US$interd==1 & US$republican==0,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$control==1 & USgCh$republican==1,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==1,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==1,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==1,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$interd==1 & USgCh$republican==1,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$control==1 & USgCh$republican==0,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$offdef==1 & USgCh$republican==0,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$secdim==1 & USgCh$republican==0,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$nukes==1 & USgCh$republican==0,]$Chisthreat)$conf.int[2], t.test(USgCh[USgCh$interd==1 & USgCh$republican==0,]$Chisthreat)$conf.int[2], t.test(China[China$control==1 & China$CCP==1,]$USisthreat)$conf.int[2],t.test(China[China$offdef==1 & China$CCP==1,]$USisthreat)$conf.int[2], t.test(China[China$secdim==1 & China$CCP==1,]$USisthreat)$conf.int[2],t.test(China[China$nukes==1 & China$CCP==1,]$USisthreat)$conf.int[2],t.test(China[China$interd==1 & China$CCP==1,]$USisthreat)$conf.int[2], t.test(China[China$control==1 & China$CCP==0,]$USisthreat)$conf.int[2],t.test(China[China$offdef==1 & China$CCP==0,]$USisthreat)$conf.int[2], t.test(China[China$secdim==1 & China$CCP==0,]$USisthreat)$conf.int[2],t.test(China[China$nukes==1 & China$CCP==0,]$USisthreat)$conf.int[2],t.test(China[China$interd==1 & China$CCP==0,]$USisthreat)$conf.int[2],t.test(ChgUS[ChgUS$control==1 &  ChgUS$CCP==1,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$offdef==1 &  ChgUS$CCP==1,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$secdim==1 &  ChgUS$CCP==1,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$nukes==1 &  ChgUS$CCP==1,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$interd==1 &  ChgUS$CCP==1,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$control==1 & ChgUS$CCP==0,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$offdef==1 & ChgUS$CCP==0,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$secdim==1 & ChgUS$CCP==0,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$nukes==1 & ChgUS$CCP==0,]$USisthreat)$conf.int[2], t.test(ChgUS[ChgUS$interd==1 & ChgUS$CCP==0,]$USisthreat)$conf.int[2]), ncol=8, byrow=F)


barplot2(matrix3, beside=T, col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90"), space=c(0,1.5), ylim=c(1,7), axes=T, ylab="Is [other country] a threat?", names.arg=c("US (2020), Rep", "US (2020), Dem", "US (2023), Rep", "US (2023), Dem", "China (2020), CCP", "China (2020), Not CCP", "China (2023), CCP", "China (2023), Not CCP"), plot.ci=T, ci.l=lower3, ci.u=upper3, xpd=F, cex=1, cex.lab=1.4, cex.names=1.4, cex.main=2, main= "Perception of Threat (7 = Major threat; 1 = No threat at all)")

segments(x0=1, x1=7, y0=u$estimate[1], y1=u$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=7.5, x1=13.5, y0=u1$estimate[1], y1=u1$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=14, x1=20, y0=uu$estimate[1], y1=uu$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=20.5, x1=26.5, y0=uu1$estimate[1], y1=uu1$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=27, x1=33, y0=i$estimate[1], y1=i$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=33.5, x1=39.5, y0=i1$estimate[1], y1=i1$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=40, x1=46, y0=ii$estimate[1], y1=ii$estimate[1], lty=3, col="BLACK", lwd=1.5)
segments(x0=46.5, x1=52.5, y0=ii1$estimate[1], y1=ii1$estimate[1], lty=3, col="BLACK", lwd=1.5)



segments(x0=1, x1=52.5, y0=1, y1=1, lty=1, col="BLACK")

legend(x=1, y=7.1, lty=c(NA, NA, NA, NA, NA, 3), lwd=c(NA, NA, NA, NA, NA, 1.5), pch=c(15,15,15,15,15, NA), col=c("GRAY30", "GRAY45", "GRAY60", "GRAY75", "GRAY90", "BLACK"), legend=c("Control group", "Offense defense", "Reciprocal", "Nuclear Weapons", "Interdependence","Mean within control group" ), bty="n", cex=1.1)


dev.off()

